package com.jiantong.lawEnforcing.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.lawEnforcing.impl.dao.WritRelevantDao;
import com.jiantong.lawEnforcing.model.WritRelevant;
import com.jiantong.lawEnforcing.service.WritRelevantService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 文书信息说明
 * Created by mz03 on 2017/8/17.
 */
@Slf4j
@Service
@RpcProvider
public class WritRelevantServiceImpl implements WritRelevantService {
    private final WritRelevantDao writRelevantDao;

    @Autowired
    public WritRelevantServiceImpl(WritRelevantDao writRelevantDao) {
        this.writRelevantDao = writRelevantDao;
    }

    /**
     * id 查找
     *
     * @param id
     * @return writRelevant
     */
    @Override
    public Response<WritRelevant> finById(Long id) {
        try {
            WritRelevant writRelevant = writRelevantDao.findById(id);
            if (writRelevant != null) {
                return Response.ok(writRelevant);
            } else {
                log.error("writRelevant (id={}) is not found", id);
                return Response.fail("writRelevant.not.found");
            }
        }catch (Exception e){
            log.error("failed to find writRelevant(id={}),cause:{}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("writRelevant.find.fail");
        }
    }

    /**
     * 创建
     *
     * @param writRelevant 单据对象
     * @return 主键 id
     */
    @Override
    public Response<Long> create(WritRelevant writRelevant) {
        try {
            writRelevantDao.create(writRelevant);
            return Response.ok(writRelevant.getId());
        }catch (Exception e) {
            log.error("failed.to.create.writRelevant:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("create.writRelevant.failed");
        }
    }

    /**
     * 更新
     *
     * @param writRelevant 单据对象
     * @return true or false
     */
    @Override
    public Response<Boolean> update(WritRelevant writRelevant) {
        try{
            return Response.ok(writRelevantDao.update(writRelevant));
        }catch (Exception e){
            log.error("failed.to.update.writRelevant:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("update.writRelevant.failed");
        }
    }
}
